package com.mju.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mju.controller.common.BaseController;
import com.mju.controller.common.JsonResult;
import com.mju.pojo.Product;
import com.mju.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/products")
public class ProductController extends BaseController {
    @Autowired
    private IProductService productService;

    @GetMapping("/hot_list")
    public JsonResult<List<Product>> getHotList() {
        List<Product> data = productService.selectHotList();
        return new JsonResult<>(OK, data);
    }

    @GetMapping("{id}/details")
    public JsonResult<Product> getById(@PathVariable("id") Integer id) {
        Product data = productService.selectById(id);
        return new JsonResult<>(OK, data);
    }

    // 处理根据产品关键字进行模糊查询的请求
    @GetMapping("/{currentPage}/{pageSize}/{title}")
    public JsonResult<IPage<Product>> quertByTitle(@PathVariable("currentPage") Integer currentPage,
                                                     @PathVariable("pageSize") Integer pageSize,
                                                     @PathVariable("title") String title){
        IPage<Product> data = productService.queryProductByTitle(currentPage, pageSize, title);
        return new JsonResult<>(OK, data);
    }

}
